package com.example.provider.javastudy.thread.concurrency;

/**
 * All rights Reserved, Designed By www.maihaoche.com
 *
 * @Package com.example.demo.thread.concurrency
 * @author: 红雪（hongxue@maihaoche.com）
 * @date: 2019/1/16 4:12 PM
 * @Copyright: 2017-2020 www.maihaoche.com Inc. All rights reserved.
 * 注意：本内容仅限于卖好车内部传阅，禁止外泄以及用于其他的商业项目
 */
public class TestDeadLock {

    public static void main(String[] args) {
        final Hero ahri = new Hero();
        ahri.name = "九尾妖狐";
        final Hero annie = new Hero();
        annie.name = "安妮";

        Thread thread1 = new Thread(()->{
            synchronized (ahri){
                System.out.println("占有1秒"+ahri.name);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (annie){
                    System.out.println("试图占有"+annie.name);
                }

            }

        });
        thread1.start();

        Thread thread2 = new Thread(()->{
            synchronized (annie){
                System.out.println("占有1秒"+annie.name);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (ahri){
                    System.out.println("试图占有"+ahri.name);
                }
            }

        });
        thread2.start();
    }
}
